package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    /**
     * @Author: WSQ
     * @Desc: restful风格保存单条数据
     * @Date: 2022/9/1 11:03
     */

    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData1(@PathVariable("name") String name, @PathVariable("age") Integer age, @PathVariable("sex") String sex) {
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        reviewService.saveData1(review);
        return AjaxResult.success("插入成功!");
    }

    /**
     * @Author: WSQ
     * @Desc: 保存jsno格式 单条数据
     * @Date: 2022/9/1 11:04
     */
    @PostMapping("/saveData")
    public AjaxResult saveData2(@RequestBody MybatisReviewVO reviewVO) {
        Review review = new Review();
        BeanUtils.copyProperties(reviewVO, review);
        reviewService.saveData1(review);

        return AjaxResult.success("插入成功!!");
    }
    /**=========================================================删除数据=============================================*/

    /**
     * @Author: WSQ
     * @Desc: 根据id 删除单条数据
     * @Date: 2022/9/1 11:06
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        reviewService.deleteById(id);

        return AjaxResult.success("删除成功!!!");
    }

    @DeleteMapping("/remove")
    public AjaxResult deleteBatch(Integer [] ids){
        reviewService.deleteBatch(ids);
        return null;
    }

    /**
     * =========================================================修改数据=============================================
     */

    /**
     * @Author: WSQ
     * @Desc: 修改数据
     * @Date: 2022/9/1 11:06
     */
    @PostMapping("/update")
    public AjaxResult upDate(@RequestBody MybatisReviewVO reviewVO) {
        Review review = new Review();
        BeanUtils.copyProperties(reviewVO, review);
        reviewService.upDate(review);

        return AjaxResult.success("修改成功~");
    }

    /**
     * =========================================================查询数据=============================================
     */

    /**
     * @Author: WSQ
     * @Desc: 分页查询
     * @Date: 2022/9/1 11:07
     */
    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage(MybatisReviewVO reviewVO) {
        startPage();

        Review review = new Review();
        BeanUtils.copyProperties(reviewVO,review);

        List<Review> list = reviewService.getDataByPage(review);
        List<MybatisReviewVO> voList = list.stream().map((review1) -> {
            MybatisReviewVO reviewVO1 = new MybatisReviewVO();
            BeanUtils.copyProperties(review1, reviewVO1);
            return reviewVO1;
        }).collect(Collectors.toList());

        return getDataTable(voList);
    }

    /**
     * @Author: WSQ
     * @Desc: 根据id查询
     * @Date: 2022/9/1 11:39
     */
    @GetMapping("/getById")
    public AjaxResult getById(Integer id){
//        List<Review> list = reviewService.getById(id);
        Review review = reviewService.getById(id);
//        MybatisReviewVO reviewVO = new MybatisReviewVO();
//        BeanUtils.copyProperties(review,reviewVO);
//        List<MybatisReviewVO> list = new ArrayList<>();
//        list.add(reviewVO);
//        return getDataTable(list);
        return AjaxResult.success(review);
    }
}